<%
/**
 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
%>

<%@ include file="/html/portlet/ext/reports/init.jsp" %>
<%@ page import="org.apache.commons.beanutils.BeanComparator"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.Arrays"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashSet"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.regex.Matcher"%>
<%@ page import="java.util.regex.Pattern"%>
<%@ page import="com.liferay.chat.service.StatusLocalServiceUtil" %>
<liferay-util:include page="/html/portlet/ext/reports/tabs1.jsp" />
<%
String tabs1 = ParamUtil.getString(renderRequest, "tabs1", "Users");
PortletURL tabs1URL = renderResponse.createRenderURL();
tabs1URL.setParameter("tabs1", tabs1);

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
List us = ReportsEntryLocalServiceUtil.getAll();
BeanComparator comp = new BeanComparator("modifiedDate");
Collections.sort(us, comp);
Collections.reverse(us);

List<Object[]> buddies = null;		
long buddiesModifiedDate = System.currentTimeMillis() - Time.MINUTE;
buddies = StatusLocalServiceUtil.getAllStatuses(themeDisplay.getUserId(), buddiesModifiedDate, 0, SearchContainer.DEFAULT_DELTA);
out.println("Online visiting Users ="+ buddies.size());

%>
<c:choose>
	<c:when test='<%= tabs1.equals("Users") %>'>
<%

SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, 20, tabs1URL, null, "");
List results = ListUtil.subList(us, searchContainer.getStart(), searchContainer.getEnd());
int total = us.size();
searchContainer.setTotal(total);
%>
	
<div class="taglib-search-iterator-page-iterator-top">
	<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" />
</div>

<table class="taglib-search-iterator" style="width:100%" cellspacing="0" cellpadding="2">
	<tr class='results-header'>
		<th>SNo </th>		
		<th>Name</th>
		<th>Country</th>
		<th>Action</th>			
	</tr>			
	<%	
		String current = null;
		Date now = new Date();
		String check = sdf.format(now);		 
		for(int i=0;i<results.size();i++){

		ReportsEntry rr = (ReportsEntry)results.get(i);	
		current = sdf.format(rr.getModifiedDate());
		
		if(check.equals(current)){
			
			if(i==0){		
			%>
			<tr><td>
			   Today (<%= sdf.format(now) %> )
			</td>						
			<%
			}
					
		}else{
		
		check = current;		
		%>
		
                <tr class='results-header'><td colspan="4">                
		    <%= sdf.format(rr.getModifiedDate()) %> 
		</td></tr>						
		<%
		
		}
		String className = ( ( i%2 == 0 ) ? "portlet-section-body results-row" : "portlet-section-body results-row alt"); 
		String classNameHover = className+" hover";
		%>
			<tr class='<%= className %>'; onmouseout="this.className = '<%=className%>'" onmouseover="this.className = '<%=classNameHover%>'"> 
			<td><%= Integer.toString(searchContainer.getStart() + i + 1)%></td>			
			<td><%=rr.getName()%></td>			
			<td><%=rr.getCountry()%></td>
			
		<td>				
			<liferay-ui:icon-menu>
				<portlet:actionURL windowState="<%= WindowState.MAXIMIZED.toString() %>" var="detailsEntryURL">
				<portlet:param name="struts_action" value="/ext/reports/view_reports" />
				<portlet:param name="<%= Constants.CMD %>"value="<%= Constants.VIEW %>" />
				<portlet:param name="entryId" value="<%= String.valueOf(rr.getEntryId()) %>" />	
				</portlet:actionURL>
				<liferay-ui:icon image="view" message="view" url="<%= detailsEntryURL %>" />
			</liferay-ui:icon-menu>
		</td>							
			</tr>
		<%			
		}
				
%>
</table>		
</c:when>

<c:when test='<%= tabs1.equals("History") %>'>
<%
String title = "Date &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; Number of Users &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Number Of Times";
%>
<div>
<table>
<tr class='results-header'>			
	<th><%=title%></th>
						
</tr>
<%
String[] strs = new String[us.size()];	
	List xyz = new ArrayList();
	List abc = new ArrayList();
	List lmn = new ArrayList();
	
	for(int i=0;i<us.size();i++){
		ReportsEntry rr = (ReportsEntry)us.get(i);		
		strs[i]=sdf.format(rr.getModifiedDate());		
		lmn.add(sdf.format(rr.getModifiedDate())+"-"+rr.getName());
	}
	String[] unique = getUniqueElements(strs);
	int[] indices = getDuplicateIndices(unique, strs);

	HashSet set1 = new HashSet();
	set1.addAll(lmn);

	for(int j = 0; j < indices.length; j++) {
		int count1=0;
		Iterator iterator1 =set1.iterator();
		while(iterator1.hasNext()){
			
			String st1 = (String) iterator1.next();					
			Pattern p = Pattern.compile(unique[j]);
			Matcher m = p.matcher(st1);
			if(m.find()){									
				count1++;		
			}		
		}
	 xyz.add(unique[j]+"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;"+count1+"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;"+indices[j]);	
	}

	
SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, 20, tabs1URL, null, "");
List resultss = ListUtil.subList(xyz, searchContainer.getStart(), searchContainer.getEnd());
int total = xyz.size();
searchContainer.setTotal(total);

	for(int i=0;i<resultss.size();i++){

	String className = ( ( i%2 == 0 ) ? "portlet-section-body results-row" : "portlet-section-body results-row alt"); 
	String classNameHover = className+" hover";
	%>
	<tr class='<%= className %>'; onmouseout="this.className = '<%=className%>'" onmouseover="this.className = '<%=classNameHover%>'"> 
	<td><%=resultss.get(i)%></td></tr>		
	<%}   
%>
<%!
    private String[] getUniqueElements(String[] allStrs) {
        String[] temp = new String[allStrs.length];
        int count = 0;
        for(int j = 0; j < allStrs.length; j++) {
            if(isUnique(allStrs[j], temp))
                temp[count++] = allStrs[j];
        }
        String[] uniqueStrs = new String[count];
        System.arraycopy(temp, 0, uniqueStrs, 0, count);
        return uniqueStrs;
    }
   
    private boolean isUnique(String s, String[] array) {
        for(int j = 0; j < array.length; j++) {
            if(array[j] != null && s.equals(array[j]))
                return false;
        }
        return true;
    }
 
    private int[] getDuplicateIndices(String[] targetStrs, String[] allStrs) {
        int[] indices = new int[targetStrs.length];
        for(int j = 0; j < targetStrs.length; j++) {
            for(int k = 0; k < allStrs.length; k++) {
                if(targetStrs[j].equals(allStrs[k]))
                    indices[j]++;
            }
        }
	
        return indices;
    }
   
%>
<tr><td>
<div class="taglib-search-iterator-page-iterator-bottom">
	<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" />
</div>
</td></tr>
</table>
</div>
</c:when>
</c:choose>
